<?php

namespace app\index\controller;

use app\common\api\controller\Error;
use app\common\model\Bguanli;
use app\common\model\Bjiandan;
use app\common\model\IncomeDay;
use app\common\model\UserPerformance;
use app\common\model\UserRelation;
use think\Db;
use app\common\api\controller\User as UserApi;
use app\common\api\controller\Bonus;
use think\Validate;

class Order extends Home
{
    protected $beforeActionList = [
        'check_load' => ['except' => 'autoconfirmorder']
    ];

    /**
     * 订单列表
     */
    public function orders()
    {
        $page = input('page');
        $start = ($page - 1) * self::$totalList;

        $uid = $this->uid;
        $status = input('status', -1);
        if ($status > -1) {
            $map['status'] = $status;
        } else {
            $map['status'] = ['>=', 0];
        }
        $orders = Db('shop_order')
//            ->select('collection_product.id', 'collection_product.product_id', 'shop.product_name', 'shop.goods_ico')
            ->where('uid', $uid)
            ->where($map)
            ->order('id desc')
            ->limit($start, self::$totalList)
            ->select();

        if ($orders) {
            foreach ($orders as &$v) {
                $lists = Db('shoplist')->where('orderid', $v['id'])->select();
                $totalNum = 0;
                if (count($lists) > 0) {
                    foreach ($lists as &$vv) {
                        $pInfo = Db('shop')->where('id', $vv['goodid'])->find();
//                        $vv['product_name'] = $pInfo['product_name'];
//                        $vv['parameter'] = ProductSku::where('id', $vv['parameter_id'])->value('value');
                        $vv['goods_introduct'] = $pInfo['goods_introduct'];
                        $vv['goods_ico'] = get_cover($pInfo['goods_ico'], 'path');
                        $totalNum += $vv['num'];
                    }
                }
                $v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $v['total_num'] = $totalNum;
                $v['product_list'] = $lists;
            }
        }

        $totalCount = Db('shop_order')
            ->where('uid', $uid)
            ->where($map)
            ->count();

        $this->apiSuccess('订单列表', ['list' => $orders, 'totalCount' => $totalCount]);
    }

    /**
     * 确认订单
     */
    public function confirmOrder()
    {
        if (request()->isPost()) {
            $id = input('id');
            $list = Db('shoplist')->where('id', $id)->find();

            if (!$list) {
                $this->apiError('该订单不存在！');
            }
            if ($list['status'] != 10) {
                $this->apiError('该订单不可确认！');
            }
            $data['status'] = 20;
            $data['finish_time'] = time();
            $res = Db('shoplist')->where('id', $id)->update($data);
            if ($res) {
                $num = Db('shoplist')->where('orderid', $list['orderid'])->where('status', 'lt', 20)->count();
                if ($num == 0) {
                    Db('shop_order')->where('id', $list['orderid'])->update($data);
                }
                $this->apiSuccess('签单成功');
            } else {
                $this->apiError('签单失败');
            }
        }
    }

    /**
     * 删除订单
     */
    public function delOrder()
    {
        if (!request()->isPost()) {
            $this->apiError('非法操作！');
        }

        $id = input('id');
        $orderInfo = Db('shop_order')->where('id', $id)->find();
        if (!$orderInfo) {
            $this->apiError('该订单不存在！');
        }
        if ($orderInfo['status'] != 20) {
            $this->apiError('该订单不可删除！');
        }

        $res = Db('shop_order')->where('id', $id)->update(['status' => -2]);
        if ($res) {
            $this->apiSuccess('删除成功');
        } else {
            $this->apiError('删除失败');
        }
    }

    /**
     * 订单信息
     * @param $map
     * @param string $mark
     */
    public function orderList($map)
    {
        $uid = is_login();

        $map['uid'] = $uid;

        $list = Db("shoplist")->where($map)->order('create_time desc')->paginate(15);
        $page = $list->render();
        $list = $list->toArray();
        foreach ($list['data'] as &$val) {
            $val['goods_ico'] = Db('shop')->where('id', $val['goodid'])->value('goods_ico');
            $val['goods_introduct'] = Db('shop')->where('id', $val['goodid'])->value('goods_introduct');
        }

        $this->assign('_list', $list['data']);// 赋值数据集
        $this->assign('page', $page);
    }
}